package com.tjsinfo.tjsoa.workplan.bo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.system.dao.ISystemUserDao;
import com.tjsinfo.tjsoa.system.vo.TSystemUser;
import com.tjsinfo.tjsoa.system.vo.User;
import com.tjsinfo.tjsoa.workplan.dao.IWorkPlanDefineDao;
import com.tjsinfo.tjsoa.workplan.dao.IibatisWorkPlanDefineDao;
import com.tjsinfo.tjsoa.workplan.vo.WorkPlanMain;
import com.tjsinfo.tjsoa.workplan.vo.WorkPlanDefine;
import com.tjsinfo.tjsoa.workplan.vo.WorkPlanExamine;

public class WorkPlanDefineBo implements IWorkPlanDefineBo{
	
	private final boolean STARTTRUE= true ;
	private final boolean STARTFALSE= false;
	IWorkPlanDefineDao workPlanDefineDao;
	private IibatisWorkPlanDefineDao iibatisWorkPlanDefineDao;
	private ISystemUserDao systemUserDao;
	private IQbcDao qbcDao;

	public IWorkPlanDefineDao getWorkPlanDefineDao() {
		return workPlanDefineDao;
	}

	public void setWorkPlanDefineDao(IWorkPlanDefineDao workPlanDefineDao) {
		this.workPlanDefineDao = workPlanDefineDao;
	}

	public IibatisWorkPlanDefineDao getIibatisWorkPlanDefineDao() {
		return iibatisWorkPlanDefineDao;
	}

	public void setIibatisWorkPlanDefineDao(
			IibatisWorkPlanDefineDao iibatisWorkPlanDefineDao) {
		this.iibatisWorkPlanDefineDao = iibatisWorkPlanDefineDao;
	}

	public ISystemUserDao getSystemUserDao() {
		return systemUserDao;
	}

	public void setSystemUserDao(ISystemUserDao systemUserDao) {
		this.systemUserDao = systemUserDao;
	}

	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.workplan.bo.IWorkPlanDefineBo#delete(com.tjsinfo.tjsoa.workplan.vo.WorkPlanDefine)
	 */
	public void delete(WorkPlanDefine workPlanDefine) {
		// TODO Auto-generated method stub
		workPlanDefineDao.delete(workPlanDefine);
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.workplan.bo.IWorkPlanDefineBo#save(com.tjsinfo.tjsoa.workplan.vo.WorkPlanDefine)
	 */
	public String save(WorkPlanDefine workPlanDefine) {
		// TODO Auto-generated method stub
		return workPlanDefineDao.save(workPlanDefine);
	}

	/* (non-Javadoc)
	 * @see com.tjsinfo.tjsoa.workplan.bo.IWorkPlanDefineBo#update(com.tjsinfo.tjsoa.workplan.vo.WorkPlanDefine)
	 */
	public void update(WorkPlanDefine workPlanDefine) {
		// TODO Auto-generated method stub
		workPlanDefineDao.update(workPlanDefine);
	}
	
	/**
	 * 查询当前人的默认提交者

	 * @param 计划类型
	 * @param 当前用户
	 * @return
	 */
	 public WorkPlanDefine selectDefineAndType(String type,Integer userid)
	{
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		WorkPlanDefine planDefine = new WorkPlanDefine();//工作计划缺省定义
		planDefine.setTjsUser(tjsUser);
		planDefine.setType(type);
		return 	workPlanDefineDao.selectDefineAndType(planDefine);
		
	}
	/**
	 * 查询当前人的默认提交者

	 * @param 当前用户
	 * @return
	 */
	public List<WorkPlanExamine> selectDefineAndTypeList(Integer userid)
	{
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		WorkPlanDefine planDefine = new WorkPlanDefine();
		planDefine.setTjsUser(tjsUser);
		return 	workPlanDefineDao.selectDefineUserNoType(planDefine);
		
	}
	/**
	 * 查询默认可以查询的上报人不用类型
	 * @param 当前用户
	 * @return
	 */
	public List selectDefineAndType(Integer userid)
	{
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		WorkPlanDefine planDefine = new WorkPlanDefine();
		planDefine.setTjsUser(tjsUser);
		List list2 = new ArrayList(); 
		List list=workPlanDefineDao.selectUserNoType(planDefine);
		Set<WorkPlanDefine> set = new HashSet<WorkPlanDefine>(list);
		list.clear();
		for (Iterator iterator = set.iterator(); iterator.hasNext();) {
			WorkPlanDefine workPlanDefine = (WorkPlanDefine) iterator.next();
			list.add(workPlanDefine);
		}
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			WorkPlanDefine workPlanDefine = (WorkPlanDefine) iterator.next();
			workPlanDefine.setTjsUserName(systemUserDao.findById(workPlanDefine.getUserId()).getUserName()); 
			list2.add(workPlanDefine);
		}
		return list2;
		
	}
	/**
	 * 批量添加工作计划默认上报人

	 * @param list
	 * @return
	 */
	/*private boolean  addDefineList(List<WorkPlanDefine> list)
	{
		try {
			iibatisWorkPlanDefineDao.addDefintlist(list);
			return STARTTRUE;
		} catch (RuntimeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
	}*/
	
	public boolean addDefine(User user,String[] userarray,String type)
	{
		/*com.tjsinfo.tjsoa.workplan.sqlmap.WorkPlanDefine planDefine = new com.tjsinfo.tjsoa.workplan.sqlmap.WorkPlanDefine();
		planDefine.setTjsUserByUserId(user.getUser_id());
		planDefine.setType(type);
		iibatisWorkPlanDefineDao.delDefintlist(planDefine);*/
		try {
			
			List<WorkPlanDefine> list = new ArrayList<WorkPlanDefine>();//工作计划省列表
			if(userarray != null)
			{
				for (String str : userarray) {
					WorkPlanDefine define = new WorkPlanDefine();
					TSystemUser tjsUser = new TSystemUser();
					tjsUser.setUserId(Integer.parseInt(str));
					define.setTjsUser(tjsUser);
					define.setUserId(user.getUser_id());
					define.setType(type);
					list.add(define);
				}
			}
			com.tjsinfo.tjsoa.workplan.sqlmap.WorkPlanDefine workPlanDefine = new com.tjsinfo.tjsoa.workplan.sqlmap.WorkPlanDefine();
			workPlanDefine.setTjsUserByUserId(user.getUser_id());
			workPlanDefine.setType(type);
			iibatisWorkPlanDefineDao.delDefintlist(workPlanDefine);
			if (list.size() > 0) {
				workPlanDefineDao.addDefintlist(list);
			}
		
		return STARTTRUE;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return STARTFALSE;
		}
		
	}

	/**
	 * 查询当前人的可以审批的人
	 * @param workPlanDefine
	 * @return
	 */
	public List selectDefineUserList(Integer userid)
	{
		WorkPlanDefine planDefine = new WorkPlanDefine();
		TSystemUser tjsUser = new TSystemUser();
		tjsUser.setUserId(userid);
		planDefine.setTjsUser(tjsUser);
		return  workPlanDefineDao.selectDefineUser(planDefine);
	}
	
	public List selectDefine(Integer userid) {
		// TODO Auto-generated method stub
		DetachedCriteria detachedCriteria  = DetachedCriteria.forClass(WorkPlanDefine.class);
		detachedCriteria.add(Restrictions.eq("userId", userid));
		List<WorkPlanDefine> list = qbcDao.getCriteria(detachedCriteria);
		ArrayList<TSystemUser> userlist = new ArrayList<TSystemUser>();
		for (WorkPlanDefine workPlanDefine : list) {
			DetachedCriteria criteria  = DetachedCriteria.forClass(TSystemUser.class);
			criteria.add(Restrictions.eq("userId", workPlanDefine.getTjsUser().getUserId()));
			TSystemUser systemUser = (TSystemUser) qbcDao.getCriteria(criteria).get(0);
			userlist.add(systemUser);
		}
		return userlist; 
	}
	
}
